package org.zuel.huhuForum.config.DruidConfig;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"org.zuel.huhuForum.dao.dc"},sqlSessionFactoryRef = "DCSqlSessionFactory")
public class DCDruidConfig {
    @Bean(name = "secDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.dc")
    public DataSource secDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "DCSqlSessionFactory")
    public SqlSessionFactory secSqlSessionFactory(@Qualifier("secDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/dc/*.xml"));
        return sessionFactoryBean.getObject();
    }

    @Bean(name = "secSqlSessionTemplate")
    public SqlSessionTemplate secSqlSessionFactoryTemplate(@Qualifier("DCSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
